<?php

/**
 * user actions.
 *
 * @package    dashboard
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->userss = Doctrine_Core::getTable('Users')
      ->createQuery('a')
      ->execute();
  }

  public function executeLogin(sfWebRequest $request)
  {
    
  }

  public function executeLoginProcess(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST));

      $this->user = new Users();

      $this->username = $this->request->getParameter('username');
      $this->password = hash_hmac('sha256', $this->request->getParameter('password'), 'rU8iC0nBi11In9');

      $this->client = $this->user->loginCheck($this->username,$this->password);
      //$this->is_client = Doctrine::getTable('Users')->loginCheck($this->username,$this->password);

      if (!$this->client['is_client']) :
         $this->forward('default', 'badLogin');
      endif;

      $this->getUser()->setAuthenticated(true);
      $this->getUser()->addCredential("Client");
      $this->getUser()->setAttribute('user_id', $this->client['id']);
      $this->getUser()->setAttribute('username',$this->client['username']);
      $this->getUser()->setAttribute('account_id',$this->client['account_id']);
      $this->getUser()->setAttribute('is_individual',$this->client['is_individual']);

      if ($this->client['is_individual']) :
        $this->redirect('invoice/index');
      else :
        $this->redirect('invoice/reporting');
      endif;
      //$this->roles = $this->user->rolesCheck($this->is_client['id']);

  }

  public function executeLogout()
  {
    $this->getUser()->setAuthenticated(false);

    $this->getUser()->clearCredentials();

    if ($this->getUser()->hasCredential('Client')) $this->getUser()->getAttributeHolder()->removeNamespace('Client');

    $this->getUser()->getAttributeHolder()->remove('user_id');
    $this->getUser()->getAttributeHolder()->remove('username');
    $this->getUser()->getAttributeHolder()->remove('account_id');
    $this->getUser()->getAttributeHolder()->remove('is_individual');

    $this->getUser()->shutdown();

    $this->redirect('user/login');
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new UsersForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new UsersForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id'))), sprintf('Object users does not exist (%s).', $request->getParameter('id')));
    $this->form = new UsersForm($users);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id'))), sprintf('Object users does not exist (%s).', $request->getParameter('id')));
    $this->form = new UsersForm($users);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id'))), sprintf('Object users does not exist (%s).', $request->getParameter('id')));
    $users->delete();

    $this->redirect('user/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $users = $form->save();

      $this->redirect('user/edit?id='.$users->getId());
    }
  }
}
